package com.it.admin.controller;

import com.it.admin.service.AdminUserService;
import com.it.common.aspect.ApiOperationLog;
import com.it.common.enums.ResponseCodeEnum;
import com.it.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.it.admin.model.dto.user.UpdateAdminUserPasswordDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin")
@Api(tags = "Admin 用户模块")
public class AdminUserController {

    @Resource
    private AdminUserService adminUserService;

    @PostMapping("/password/update")
    @ApiOperation(value = "修改用户密码")
    @ApiOperationLog(description = "修改用户密码")
    @PreAuthorize(value = "hasRole('admin')")
    public Response updatePassword(@RequestBody @Validated UpdateAdminUserPasswordDto updateAdminUserPasswordDto) {

        String password = updateAdminUserPasswordDto.getPassword();
        String confirmPassword = updateAdminUserPasswordDto.getRePassword();
        if(!password.equals(confirmPassword)){
            return Response.fail(ResponseCodeEnum.PASSWORD_NOT_EQUALLY);
        }
        return adminUserService.updatePassword(updateAdminUserPasswordDto);
    }

    @GetMapping("/user/info")
    @ApiOperation(value = "获取用户信息")
    @ApiOperationLog(description = "获取用户信息")
    public Response findUserInfo() {
        return adminUserService.findUserInfo();
    }
}
